/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.AttributeDao;
import com.scpii.universal.hibernate.entity.AttributeTab;

/**
 * genericWeb
 * @author weny
 * 2012-7-6上午10:45:44
 */
@Repository
public class AttributeDaoImpl extends BaseDaoImpl<AttributeTab, Integer> implements
		AttributeDao {

	@Override
	public void update(AttributeTab obj) {
		String hql = "update AttributeTab a set a.datUpdateDatetime=?,a.varAttriName=? where a.intAttriId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(
						0,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(1,
						obj.getVarAttriName()).setParameter(2,
						obj.getIntAttriId()).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AttributeTab> getAttributesByCateId(String cateIds, String[] properties) {
		StringBuffer hql = new StringBuffer();
		hql.append(generateHqlByProperties(properties));
		hql.append(" where entity.intDeleteSign=? and entity.intCateId in");
		hql.append(cateIds);
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString()).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).list();
	}

	@Override
	public void delete(String ids) {
		String hql = "update AttributeTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intCateId in" + ids;
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(new Date())).executeUpdate();
	}

	@Override
	public void delete(Integer id) {
		String hql = "update AttributeTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intAttriId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(new Date())).setParameter(2, id).executeUpdate();
	}
}
